// MessageEditArea.h
// Created by Limou on 24-8-17.
//

#ifndef FRONTEND_MESSAGEEDITAREAA_H
#define FRONTEND_MESSAGEEDITAREAA_H

#include <QWidget>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QVBoxLayout>
#include <QVBoxLayout>
#include <QScrollBar>
#include "../../common/Common.hpp"
#include "../MessageShowArea/MessageShowArea.hpp"
#include "../../model/MessageModel.hpp"

// >>> class MessageEditArea begin >>>
namespace chat_system::subassembly {
    class MessageEditArea : public QWidget {
    Q_OBJECT
    private:
        QPushButton* shareOutward_; // 上传按钮部件
        QPushButton* voice_; // 语音按钮部件
        QPushButton* chattingRecords_; // 历史按钮部件
        QPlainTextEdit* messageEdit_; // 编辑区域部件
        QPushButton* sendTextBtn_; // 发送按钮部件 TODO: 以后命名都要带上部件类

    public:
        // 构造方法
        explicit MessageEditArea(QWidget *parent = nullptr)
                : QWidget(parent)
                , shareOutward_(new QPushButton(this))
                , voice_(new QPushButton(this))
                , chattingRecords_(new QPushButton(this))
                , messageEdit_(new QPlainTextEdit(this))
                , sendTextBtn_(new QPushButton(this)) {
            this->setElement(); // 设置组件
            this->setElementStyle(); // 设置组件样式
            this->setElementLayout(); // 设置组件布局
            this->setElementSignalSlot(); // 设置信号槽
        }


        // 析构方法
        ~MessageEditArea() override = default;

    private:
        // 设置组件方法
        void setElement() {
            // 设置本部件
            this->setFixedHeight(200); // TODO: 改为用户可以动态调整
            this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

            QSize btnSize(35, 35);
            QSize iconSize(25, 25);

            QIcon shareOutward(":/window/window_right/share_outward.png");
            if (shareOutward.isNull()) { common::Tools::getInstance()->getLogger()->warn("shareOutward icon is null"); }
            shareOutward_->setFixedSize(btnSize);
            shareOutward_->setIconSize(iconSize);
            shareOutward_->setIcon(shareOutward);

            QIcon voice(":/window/window_right/voice.png");
            if (voice.isNull()) { common::Tools::getInstance()->getLogger()->warn("voice icon is null"); }
            voice_->setFixedSize(btnSize);
            voice_->setIconSize(iconSize);
            voice_->setIcon(voice);

            QIcon chattingRecords(":/window/window_right/chatting_records.png");
            if (chattingRecords.isNull()) { common::Tools::getInstance()->getLogger()->warn("chattingRecords icon is null"); }
            chattingRecords_->setFixedSize(btnSize);
            chattingRecords_->setIconSize(iconSize);
            chattingRecords_->setIcon(chattingRecords);

            // 设置编辑区域
            messageEdit_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

            // 设置发送按钮
            sendTextBtn_->setText("发送");
            sendTextBtn_->setFixedSize(120, 35);
        }

        // 设置组件样式方法
        void setElementStyle() {
            this->setStyleSheet("QWidget { border-top: none; border-bottom: none; }");
            QString btnStyle =
                    "QPushButton {"
                    "background-color: rgb(245, 245, 245);"
                    "border: none;"
                    "}"
                    "QPushButton:pressed {"
                    "background-color: rgb(255, 255, 255);"
                    "border: none;"
                    "}";
            shareOutward_->setStyleSheet(btnStyle);
            voice_->setStyleSheet(btnStyle);
            chattingRecords_->setStyleSheet(btnStyle);
            messageEdit_->verticalScrollBar()->setStyleSheet(
                    "QScrollBar:vertical {"
                    "width: 5px;"
                    "background-color: transparent;"  // 滚动条背景透明
                    "}"
                    "QScrollBar::handle:vertical {"
                    "background: rgb(46, 46, 46);"  // 滚动条的滑块颜色
                    "}");
            messageEdit_->setStyleSheet(
                    "QPlainTextEdit {"
                    "color: black;"
                    "font-size: 14px;"
                    "padding: 10px;"
                    "border: none;"
                    "}"
            );
            sendTextBtn_->setStyleSheet(
                    "QPushButton {"
                    "color: rgb(7, 193, 96);"
                    "border: none;"
                    "font-size: 15px;"
                    "background-color: rgb(233, 233, 233);"  // 默认背景颜色
                    "border-radius: 5px;"
                    "}"
                    "QPushButton:hover {"
                    "background-color: rgb(210, 210, 210);"  // 鼠标悬停时的背景颜色
                    "}"
                    "QPushButton:pressed {"
                    "background-color: rgb(198, 198, 198);"  // 鼠标按下时的背景颜色
                    "}"
            );
        }

        // 设置组件布局方法
        void setElementLayout() {
            auto vlayout = new QVBoxLayout();
            auto hlayout = new QHBoxLayout();

            // 设置布局管理器
            vlayout->setSpacing(0);
            vlayout->setContentsMargins(10, 0, 10, 10);
            this->setLayout(vlayout);

            hlayout->setSpacing(0);
            hlayout->setContentsMargins(10, 0, 0, 0);
            hlayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);

            hlayout->addWidget(shareOutward_);
            hlayout->addWidget(voice_);
            hlayout->addWidget(chattingRecords_);

            vlayout->addLayout(hlayout);
            vlayout->addWidget(messageEdit_);
            vlayout->addWidget(sendTextBtn_, 0, Qt::AlignRight | Qt::AlignBottom);
        }

        // 设置信号槽
        void setElementSignalSlot() {
            connect(shareOutward_, &QPushButton::clicked, this, [this](){
                common::Tools::getInstance()->getLogger()->trace("{0} {1} shareOutward_ is clicked", __FILE__, __LINE__);
            });

            connect(voice_, &QPushButton::clicked, this, [this](){
                common::Tools::getInstance()->getLogger()->trace("{0} {1} voice_ is clicked", __FILE__, __LINE__);
            });

            connect(chattingRecords_, &QPushButton::clicked, this, [this](){
                common::Tools::getInstance()->getLogger()->trace("{0} {1} chattingRecords_ is clicked", __FILE__, __LINE__);
                // TODO: 弹出屏幕中心的一个窗口
                // TODO: 出现搜索和消息分类限定词语
                // TODO: 出现消息列表
            });

            connect(sendTextBtn_, &QPushButton::clicked, this, [=]() {
                common::Tools::getInstance()->getLogger()->trace("{0} {1} sendTextBtn_ is clicked", __FILE__, __LINE__);
#ifdef TEST
                auto user = model::UserModel::makeUser(
                        "Limou",
                        "123456",
                        "Coding...",
                        QIcon(":/test/limou.png")
                );
                auto message = model::MessageModel::makeMessage(
                        "1",
                        common::MessageType::TEXT_TYPE,
                        user,
                        QString(messageEdit_->toPlainText()).toUtf8()
                );
#endif

                // 遍历父对象的所有子对象，找到类型为 MessageShowArea 的
                QWidget *parent = parentWidget();
                if (!parent) {
                    return;
                }

                QObjectList children = parent->children();
                for (auto child : children) {
                    if (subassembly::MessageShowArea* messageShowArea = qobject_cast<subassembly::MessageShowArea*>(child)) { // 若转化成功
                        messageShowArea->addTailTextMessageBubble(false, message); // 把编辑区的消息发送出去
                        messageEdit_->setPlainText(""); // 清空编辑区
                    }
                }
            });
        }
    }; // class MessageEditArea end
} // namespace chat_system::subassembly end
// <<< class MessageEditArea end <<<

#endif // FRONTEND_MESSAGEEDITAREAA_H
